﻿using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.Menu;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.Menu;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendAPI.Service
{
    public class MenuService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public MenuService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取菜单列表
         */
        public async Task<RequestResponseModel<PageResult<MenuView>>> GetMenuList(MenuSearchDTO menuSearchDTO)
        {
            //筛选
            var query = _context.MenuView
                .WhereIF(a => a.Name.Contains(menuSearchDTO.Name), !string.IsNullOrEmpty(menuSearchDTO.Name))
                .WhereIF(a => a.Url.Contains(menuSearchDTO.Url), !string.IsNullOrEmpty(menuSearchDTO.Url))
                .Where(a => a.Show == true)
                ;

            //返回列表
            var MenuList = await query
                .OrderBy(a => a.CreateTime)
                .Page(menuSearchDTO.CurrentPage, menuSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<MenuView>(MenuList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增菜单
         */
        public async Task<RequestResponseModel<string>> AddMenu(MenuAddDTO menuAddDTO)
        {
            //操作数据
            DateTime date = DateTime.Now;
            Guid guid = Guid.NewGuid();

            var menu = new MenuModel
            {
                Id = guid.ToString(),
                Name = menuAddDTO.Name,
                Icon = menuAddDTO.Icon,
                SuperiorId = menuAddDTO.SuperiorId,
                Show = menuAddDTO.Show,
                Url = menuAddDTO.Url,
                CreateTime = date
            };

            //新增菜单
            _context.Menu.Add(menu);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 编辑菜单
         */
        public async Task<RequestResponseModel<string>> PutMenu(MenuUpdateDTO menuUpdateDTO)
        {
            //查找数据
            var query = await _context.Menu.SingleOrDefaultAsync(a => a.Id == menuUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", menuUpdateDTO.Id);
            };

            //生成新数据
            query.Name = menuUpdateDTO.Name;
            query.Icon = menuUpdateDTO.Icon;
            query.SuperiorId = menuUpdateDTO.SuperiorId;
            query.Show = menuUpdateDTO.Show;
            query.Url = menuUpdateDTO.Url;

            //更新菜单表
            _context.Menu.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", menuUpdateDTO.Id);
        }

        /**
         * 删除菜单
         */
        public async Task<RequestResponseModel<string>> DeleteMenu(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.Menu.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.Menu.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
